package com.kirayous.task3.testUtils;


import com.kirayous.task3.entity.regionState;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVRecord;
import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.Resource;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.io.*;
import java.util.ArrayList;
import java.util.List;


@Slf4j
@SpringBootTest
public class testCommonCVS {

    private static final String CONRNA_VIRUS_DATA_URL = "https://gitee.com/dgut-sai/COVID-19/raw/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv";
    private final String AGENT="User-Agent";
    private final String AGENT_VALUE="Mozilla/5.0";
    private List<regionState> list=new ArrayList<>();

    @Test
    public void test() throws IOException {



        /*爬取数据*/
        RequestEntity<Void> requestEntity= RequestEntity
                .get(CONRNA_VIRUS_DATA_URL)
                .headers(httpHeaders -> httpHeaders.add(AGENT,AGENT_VALUE))
                .build();
        ResponseEntity<Resource> exchange= new RestTemplate().exchange(requestEntity,Resource.class);//exchange 方法既可以发送post请求也可以发送get请求

        Resource body= exchange.getBody();
        InputStream inputStream =body.getInputStream();
        Reader reader=null;

        final String key[]={"Province/State","Country/Region","Lat","Long"};

        Iterable<CSVRecord> records = CSVFormat.DEFAULT
                .withHeader(key).withFirstRecordAsHeader().parse(new InputStreamReader(inputStream));


        /*将数据存进model——regionState中*/
        for (CSVRecord record : records) {
            String state = record.get("Province/State");
            String region= record.get("Country/Region");
            int lat= Integer.parseInt(record.get(record.size()-1));
            int dpd= Integer.parseInt(record.get(record.size()-1))-Integer.parseInt(record.get(record.size()-2));
            list.add(new regionState(state,region,lat,dpd));

        }
        log.info("list:"+list);



    }



}
